<?php
namespace App\Logic\File\ProfitLT;

use App\Models\MonthlyReportLT\Tmp\TmpServiceFee;
class ParseServiceFeeFile
{
    /**
     * @param $lines
     * @param $date
     * @return int
     */
    public static function exeParse($lines,$date): int
    {
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addServiceFee($line,$date);
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addServiceFee($temp,$date): bool
    {
        if(count($temp) < 3)return true;
        if(!$temp[0])return true;
        $type = strtolower($temp[0]);
        if(!in_array($type,TmpServiceFee::$_feeType))return false;
        //服务费（临时）
        $re = TmpServiceFee::query()->create([
            'report_date' => $date,
            'type' => $type,
            'fee' => $temp[1],
        ]);
        return (bool)$re;
    }

}
